import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
@Component({
	selector: 'sa-repairRecord',
	templateUrl: './repairRecord.component.html',
})
export class RepairRecordComponent implements OnInit {
	tableData:any[] = []; //table数据
	resultTableData:any[] = [] //客户端分页数据暂存
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime:string = '';//开始时间
	endTime:string = '';	//结束时间
	recordType='';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	inputCompanyTreeCarData:any = {};//车辆运行状态模块跳转该页面传递过来的参数
	InputTime:any = {
		startTime:'',
		endTime:''
	};//父级传子组件的时间
	constructor(private jsonApiService: JsonApiService,private router: Router,private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.inputCompanyTreeCarData = JSON.parse(common.getCookie('jumpDataFixList'));
		});
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	} 
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	//刷新
	refresh(){
		this.load();
	}
	ngOnInit() {
		if(!this.InputTime.endTime){
			this.endTime = common.getNowTime()
			this.InputTime.endTime = common.getNowTime()
		}
		if(!this.InputTime.startTime){
			var day1 = new Date();
            var lastTime = day1.setTime(day1.getTime()-24*60*60*1000);
			this.startTime = this.timeFormat(lastTime)
			this.InputTime.startTime = this.timeFormat(lastTime)
		}

		if(common.getCookie('jumpDataFixList')){
			this.recordType = this.inputCompanyTreeCarData.type;
			this.searchKey = this.inputCompanyTreeCarData.carNumber;
			this.startTime = this.InputTime.startTime = this.inputCompanyTreeCarData.startTime;
			this.endTime = this.InputTime.endTime = this.inputCompanyTreeCarData.endTime;
			this.load();
			common.clearCookie('jumpDataFixList');
		}else{
			this.load();
		}
	}
	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//加载list数据
	load() {
		var data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				startTime:this.startTime,
				endTime:this.endTime,
				companyId:this.selectedCompanyId,
				searchKey:this.searchKey,
				operation:this.recordType,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http('fixdatalog/logforpage.html', reqdata).subscribe(result => {
			this.resultTableData=result.exhibitDatas;
			this.totalCount=result.recordCount;
		})
	}
	table_search(){
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
	}
	client_pages(start,length){
		this.resultTableData = this.tableData.slice(start,start+length);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}

	// 数据导出
	exportData(){
		const param = {
			startTime:this.startTime,
			endTime:this.endTime,
			companyId:this.selectedCompanyId,
			searchKey:this.searchKey,
			operation:this.recordType,
		}
		let timeCheck = common.correctTime(param)
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(param, 'param', config.BaseURL + 'fixdatalog/exportLogforpage.html')		
	}
	
	add0(m){
		return m<10?'0'+m:m 
	}
	timeFormat(ts){
		var time = new Date(ts);
		var y = time.getFullYear();
		var m = time.getMonth()+1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y+'-'+this.add0(m)+'-'+this.add0(d)+' '+this.add0(h)+':'+this.add0(mm)+':'+this.add0(s);

	}
}